<template>
	<view class="container">
		<view class="top-section">
			<view class="scan-section">
				<view class="s-input-box flex-row-between s-w">
					<input auto-focus="true" v-model="scanCode" class="s-input scan-input flex1" @confirm="handleScan"
						placeholder="请输入或扫描托盘号" />
					<image v-if="scanCode" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="scanCode=''"></image>
					<image class="s-input-icon s-i" src="/static/images/scan.png"></image>
				</view>
			</view>

			<!-- Order info section -->
			<!-- <view class="info-section" v-if="trayNo">
				<view class="info-item">
					<text class="info-label">订单号：</text>
					<text class="info-value">{{ orderInfo.orderNo }}</text>
				</view>
				<view class="info-item">
					<text class="info-label">托盘号：</text>
					<text class="info-value">{{ trayNo }}</text>
				</view>
			</view> -->
		</view>
		<scroll-view scroll-y class="materials-list" v-if="materials.length">
			<view class="material-card" v-for="(item, index) in materials" :key="index">
				<view class="material-content">
					<view class="material-row">
						<text class="label">物料编号</text>
						<text class="value">{{ item.productNo }}</text>
					</view>
					<view class="material-row">
						<text class="label">物料名称</text>
						<text class="value">{{ item.productName }}</text>
					</view>
					<view class="material-row">
						<text class="label">数量</text>
						<text class="value">{{ item.qty }}</text>
					</view>
					<view class="material-row action-row">
						<view class="action-right">
							<text 
								class="action-btn empty-btn"
								:class="{'loading': item.loading}"
								@tap="handleItemEmpty(item)"
							>解绑</text>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="nodata-label" v-else>
			<text class="label">暂无托盘信息</text>
		</view>

		<view class="fixed-bottom">
			<button class="submit-btn" @tap="handleSubmit">清 空</button>
		</view>
	</view>
</template>

<script>
	import {
		getBillTrayDetailList,
		clearBillTrayByNo,
		clearBillTrayItem,
	} from "./api"
	export default {
		data() {
			return {
				scanCode: '',
				newTrayCode: '',
				trayNo: '',
				materials: []
			};
		},
		methods: {
			handleScan() {
				if (!this.scanCode) {
					this.$modal.msg("请输入正确托盘号")
					return
				}

				getBillTrayDetailList({
					trayNo: this.scanCode,
				}).then(res => {
					if (res.code == 200) {
						if (res.total != 0) {
							this.materials = res.data;
						}
					}else{
						res.msg && this.$modal.confirmClose(res.msg)
					}
				});
			},
			handleSubmit() {
				if(!this.scanCode){
					this.$modal.msg('请扫描托盘')
					return
				}
				// 清空托盘
				this.$modal.confirm('是否确认清空').then(() => {
					this.$modal.loading("清空中...")
					clearBillTrayByNo(this.scanCode).then(res => {
						this.$modal.closeLoading()
						if (res.code == 200) {
							this.scanCode = '';
							this.materials = [];
							this.$modal.msg(res.msg)
						}else{
							res.msg && this.$modal.confirmClose(res.msg)
						}
					});
				})
			},
			handleItemEmpty(item) {
				this.$modal.confirm(`是否确认解绑物料【${item.productName}】？`).then(() => {
					this.$modal.loading("解绑中...");
					clearBillTrayItem({
						trayNo: this.scanCode,
						productNo: item.productNo,
						batchNo: item.batchNo
					}).then(res => {
						this.$modal.closeLoading();
						if (res.code == 200) {
							this.$modal.msg(res.msg);
							// 从列表中移除已清空的物料
							const index = this.materials.findIndex(m => m.productNo === item.productNo);
							if (index !== -1) {
								this.materials.splice(index, 1);
							}
							// 如果所有物料都已清空，清空托盘
							if (this.materials.length === 0) {
								this.scanCode = '';
							}
						} else {
							res.msg && this.$modal.confirmClose(res.msg);
						}
					});
				});
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #F0F2F5;
		position: relative;
		padding-bottom: 240rpx; // 为固定底部区域留出空间

		.top-section {
			padding: 30rpx 20rpx;
			background: linear-gradient(180deg, #99cfd6 0%, #4599ad 100%) !important;
		}

		.scan-section {
			background-color: #FFFFFF;
			padding: 24rpx;
			border-radius: 16rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
			margin-bottom: 20rpx;

			.scan-title {
				font-size: 28rpx;
				color: #333;
				margin-bottom: 16rpx;
				font-weight: 500;
			}
		}

		.scan-input {
			background-color: #F8F9FA;
			height: 80rpx;
			padding: 0 24rpx;
			border-radius: 8rpx;
			font-size: 28rpx;
			border: 2rpx solid #E5E6EB;

			&::placeholder {
				color: #C9CDD4;
			}
		}

		.info-section {
			background-color: #FFFFFF;
			padding: 24rpx;
			border-radius: 16rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

			.info-item {
				display: flex;
				align-items: center;
				margin-bottom: 16rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.info-label {
					font-size: 28rpx;
					color: #666;
					width: 120rpx;
				}

				.info-value {
					font-size: 28rpx;
					color: #333;
					font-weight: 500;
				}
			}
		}

		.materials-list {
			padding: 20rpx;
			max-height: calc(100vh - 400rpx);

			.material-card {
				background-color: #FFFFFF;
				border-radius: 12rpx;
				margin-bottom: 20rpx;
				box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
				overflow: hidden;

				.material-content {
					padding: 24rpx;
				}

				.material-row {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 16rpx;
					font-size: 28rpx;

					&:last-child {
						margin-bottom: 0;
					}

					.label {
						color: #666;
						width: 140rpx;
					}

					.value {
						color: #333;
						flex: 1;
						text-align: right;
					}

					&.action-row {
						margin-top: 20rpx;
						padding-top: 20rpx;
						border-top: 2rpx solid #F5F5F5;
						justify-content: flex-end;

						.action-right {
							display: flex;
							justify-content: flex-end;
							
							.action-btn {
								font-size: 26rpx;
								padding: 6rpx 24rpx;
								border-radius: 10rpx;
								
								&.empty-btn {
									color: #4599ad;
									border: 2rpx solid #4599ad;
									background: rgba(255, 77, 79, 0.05);
									
									&.loading {
										opacity: 0.7;
										pointer-events: none;
									}
									
									&:active {
										opacity: 0.7;
									}
								}
							}
						}
					}
				}
			}
		}

		.fixed-bottom {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			background-color: #FFFFFF;
			padding: 24rpx 20rpx;
			box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.05);

			.bottom-section {
				margin-bottom: 24rpx;

				.scan-title {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 16rpx;
					font-weight: 500;
				}
			}
		}

		.submit-btn {
			height: 88rpx;
			line-height: 88rpx;
			background: #4599ad;
			color: #FFFFFF;
			border-radius: 10rpx;
			font-size: 32rpx;
			text-align: center;
			font-weight: 500;
			box-shadow: 0 4rpx 12rpx rgba(46, 92, 254, 0.3);

			&:active {
				transform: scale(0.98);
			}
		}
	}

	.nodata-label {
		text-align: center;
		margin-top: 40rpx;
		color: #999999;
	}
	.s-input-box {
			position: relative;
			display: flex;
			align-items: center;
			
			.s-input {
				flex: 1;
				padding-right: 100rpx;
			}
			
			.s-input-icon {
				position: absolute;
				width: 40rpx;
				height: 40rpx;
				top: 50%;
				transform: translateY(-50%);
				z-index: 1;
				
				&.clear-icon {
					right: 80rpx;
				}
				
				&:last-child {
					right: 20rpx;
				}
			}
		}
</style>